var util = require('../utils/util');
var daoUtil = require('../utils/daoUtil');
var stockGroup = require('../class/stockGroup');

module.exports = {

    create: function (params, cb) {
        var validate = daoUtil.validate(['content', 'createUid', 'attachmentName','attachmentUrl'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var stockGroup = new stockGroup();
            stockGroup.init(params);
            models.stock_group.create(stockGroup.getParams(), function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    addChannels:function(params, cb){
        var that = this;
        daoUtil.proxy(function(models) {
            models.stock_channel.create(params, function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                } else {

                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    get:function (params,cb) {
        var validate = daoUtil.validate(['rowId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.stock_group.findOne(params).populate('createUid').populate('industry').populate('stockGroupRates').populate('stockChannelIds').exec(function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{

                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    list: function (params, cb) {
        var query = {};
        if (params.createUid)
            query.createUid = params.createUid;
        if (params.groupId)
            query.rowId = params.groupId;
        daoUtil.proxy(function(models){
            models.stock_group.find({where:query,sort:'createdAt desc'}).populate('createUid').populate('industry').populate('stockChannelIds').populate('stockGroupRates').exec(function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    /**
     * 查询组合最小交易日期
     * @param params
     * @param cb
     */
    findMinDealDate:function (params, cb) {
        var validate = daoUtil.validate(['stockGroupId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        var sql = "select min(a.dealDate) dealDate from tb_stock_deal a LEFT JOIN tb_stock_channel b on a.stockChannelId = b.rowId  LEFT JOIN tb_stock_group c on b.stockGroupId = c.rowId where c.rowId = '"+params.stockGroupId+"'";
        daoUtil.proxy(function(models) {
            models.stock_group.query(sql, function (err, data) {
                if (err || !data) {
                    console.log(err);
                    cb(util.getSysError());
                } else {

                    cb(null, util.getSuccessRes(data));
                }

            })
        },cb)
    },
    /**
     * 查询组合当前日期前股票信息
     * @param params
     * @param cb
     */
    findChannelNumByDate:function (params, cb) {
        var validate = daoUtil.validate(['stockGroupId','date'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        var sql = "select sum(a.num)nums,d.stockCode stockCode from tb_stock_deal a LEFT JOIN tb_stock_channel b on a.stockChannelId = b.rowId  LEFT JOIN tb_stock_group c on b.stockGroupId = c.rowId  LEFT JOIN tb_dic_stock d on b.stockId = d.rowId where a.dealDate <'"+params.date+"' and b.stockGroupId = '"+params.stockGroupId+"' group by a.stockChannelId  order by stockCode";
        console.log(sql+"---------------");
        daoUtil.proxy(function(models) {
            models.stock_group.query(sql, function (err, data) {
                if (err || !data){
                    console.log(err);
                    cb(util.getSysError());
                } else {

                    cb(null, util.getSuccessRes(data));
                }

            })
        },cb);
    },
    deleteByIds:function (rowIds, cb) {
        daoUtil.proxy(function(models){
            models.stock_group.destroy({rowId:rowIds}).exec(function (err, result) {
                if (err) {
                    console.log(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(result));
                }
            });
        },cb);
    }
};